package casbininit

import (
	"fmt"
	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

var Ecasbin *casbin.Enforcer

func init() {
	dbuser := fmt.Sprintf("%v", viper.Get("mysql.user"))
	dbpwd := fmt.Sprintf("%v", viper.Get("mysql.pwd"))
	dbserver := fmt.Sprintf("%v", viper.Get("mysql.server"))
	dbport := fmt.Sprintf("%v", viper.Get("mysql.port"))
	dbname := fmt.Sprintf("%v", viper.Get("mysql.name"))
	a, err := gormadapter.NewAdapter("mysql", dbuser+":"+dbpwd+"@tcp("+dbserver+":"+dbport+")/"+dbname, true)
	if err != nil {
		logrus.Panic("casbin连接mysql失败", err)
	}
	Ecasbin, err = casbin.NewEnforcer("config/casbin.conf", a)
	if err != nil {
		logrus.Panic("casbin读取配置文件失败", err)
	}

}
